<?php
class Apps_Acl extends Zend_Acl {
	/*
	 * add Role     -- Apps_Acl
	 * add Resource -- Apps_Acl
	 * allow role ,  source
	 */
	public function __construct($groupId) {
		$tbGroupAction = new Model_DbTable_GroupAction();
		$tbUserGroup   = new Model_DbTable_UserGroup();
		$d	  	 	   = $tbUserGroup->fetchRow(array('id' => $groupId));
		$role 	 	   = $d['title'];
		$this->addRole(new Zend_Acl_Role($role));

		//Add Resources
		$module 	= null;
		$controller = null;
		$action 	= null;
		$resources 	= $tbGroupAction->getResources();

		foreach ($resources as $key => $value) {
			foreach ($value as $k => $v) {
				if($k == 'module' && $v != $module) {
					$module = $v;
					$this->addResource(new Zend_Acl_Resource($module));
				}
				if($k == 'controller'
					&& $v != $controller
					&& $this->get($module)) {
						$controller = $v;
						$this->addResource(new Zend_Acl_Resource($module . ' : ' . $controller, $module));
				}
			}
		}

		$module 	= null;
		$controller = null;
		$action 	= null;
		$rs 		= $tbGroupAction->getGroupActionDetail($groupId);

		foreach ($rs as $key => $value) {
			foreach ($value as $k => $v) {
				if($k == 'module' && $v != $module) {
					$module = $v;
				}
				if($k == 'controller'
					&& $v != $controller
					&& $this->get($module)) {
						$controller = $v;
				}
				if($k == 'action') {
					$action = $v;
					$this->allow($role, $module . ' : ' . $controller, $action);//allow author
				}
			}
		}
	}
}